// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.datasource;

import java.util.ArrayList;
import java.util.List;

import org.dengues.core.resource.DenguesProject;
import org.dengues.core.resource.DenguesProjectFactory;
import org.dengues.core.resource.WarehouseResourceFactory;
import org.dengues.model.project.ConnPropertySetType;
import org.dengues.model.project.ProjectType;
import org.dengues.ui.query.QueryUiUtils;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.SelectionDialog;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-12-15 qiang.zhang $
 * 
 */
public class ProjectConnsSelectDialog extends SelectionDialog {

    private CheckboxTreeViewer zViewer;

    private final List<DenguesProject> projects;

    private Button selectAll;

    private Button selectNone;

    /**
     * Qiang.Zhang.Adolf@gmail.com TablesSelectDialog constructor comment.
     * 
     * @param parentShell
     */
    public ProjectConnsSelectDialog(Shell parentShell, List<DenguesProject> projects) {
        super(parentShell);
        setShellStyle(getShellStyle() | SWT.RESIZE | SWT.DIALOG_TRIM | SWT.MAX);
        setBlockOnOpen(true);
        this.projects = projects;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.dialogs.SelectionStatusDialog#configureShell(org.eclipse.swt.widgets.Shell)
     */
    @Override
    protected void configureShell(Shell shell) {
        super.configureShell(shell);
        shell.setText("Select Connections");
        shell.setSize(350, 400);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Control createDialogArea(Composite parent) {
        Composite clientArea = (Composite) super.createDialogArea(parent);
        clientArea.setFont(parent.getFont());
        setMessage("Select connections from others projects.");
        createMessageArea(clientArea);
        Group group = new Group(clientArea, SWT.NONE);
        GridLayout gridLayout = new GridLayout(2, false);
        group.setLayout(gridLayout);
        GridData gridData = new GridData(GridData.FILL_BOTH);
        group.setLayoutData(gridData);
        zViewer = QueryUiUtils.getInstance().createDbDiagramCheckBoxTree(group, projects, false);
        zViewer.setAllChecked(true);

        Composite composite = new Composite(group, SWT.NONE);
        gridLayout = new GridLayout();
        composite.setLayout(gridLayout);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        composite.setLayoutData(gridData);

        selectAll = new Button(composite, SWT.PUSH);
        selectAll.setText("Select  All");
        selectAll.setFont(parent.getFont());
        selectAll.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                selectAll(true);
            }

        });

        selectNone = new Button(composite, SWT.PUSH);
        selectNone.setText("Deselect All");
        selectNone.setFont(parent.getFont());
        selectNone.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                selectAll(false);
            }

        });

        return clientArea;
    }

    private void selectAll(boolean all) {
        zViewer.setAllChecked(all);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#okPressed()
     */
    @Override
    protected void okPressed() {
        Object[] checkedElements = zViewer.getCheckedElements();
        DenguesProject denguesProject = DenguesProjectFactory.getDenguesProject();
        ProjectType projectType = denguesProject.getProjectType();
        for (Object object : checkedElements) {
            if (object instanceof ConnPropertySetType) {
                ConnPropertySetType setType = (ConnPropertySetType) object;
                if (!getConnProps(projectType).contains(setType.getId())) {
                    projectType.getConnProperties().add(setType);
                }
            }
        }
        WarehouseResourceFactory.save(projectType);
        super.okPressed();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getConnProps".
     * 
     * @param projectType
     * @return
     */
    private List<String> getConnProps(ProjectType projectType) {
        List<String> list = new ArrayList<String>();
        EList<ConnPropertySetType> connProperties = projectType.getConnProperties();
        for (ConnPropertySetType connPropertySetType : connProperties) {
            list.add(connPropertySetType.getId());
        }
        return list;
    }
}
